package com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.bo.ITXingzhengshouwenNeirongBO;
import com.tjsinfo.tjsoa.documentationflow.xingzhengshouwen.vo.TXingzhengshouwenNeirong;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class JingShouRenSelectAction  extends Action{
	private ITXingzhengshouwenNeirongBO xingzhengshouwenNeirongBO;
	private ISystemUserBo systemUserBo;
	public ISystemUserBo getSystemUserBo() {
		return systemUserBo;
	}
	public void setSystemUserBo(ISystemUserBo systemUserBo) {
		this.systemUserBo = systemUserBo;
	}
	public ITXingzhengshouwenNeirongBO getXingzhengshouwenNeirongBO() {
		return xingzhengshouwenNeirongBO;
	}

	public void setXingzhengshouwenNeirongBO(
			ITXingzhengshouwenNeirongBO xingzhengshouwenNeirongBO) {
		this.xingzhengshouwenNeirongBO = xingzhengshouwenNeirongBO;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		WorkFlowTemplate workFlowTemplate = new WorkFlowTemplate();
		String path = request.getContextPath();
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		List<TXingzhengshouwenNeirong> list;
		DetachedCriteria criteria = DetachedCriteria.forClass(TXingzhengshouwenNeirong.class);
		
		Disjunction disjunctionOr = Restrictions.disjunction();
		
		if(request.getParameter("laiwenjihuan") != null && !"".equals(request.getParameter("laiwenjihuan"))){
			disjunctionOr.add(Restrictions.like("laiwenjiguan", "%"+request.getParameter("laiwenjihuan")+"%"));
		}
		if(request.getParameter("laiwenzihao") != null && !"".equals(request.getParameter("laiwenzihao"))){
			disjunctionOr.add(Restrictions.like("laiwenzihao", "%"+request.getParameter("laiwenzihao")+"%"));
		}
		if(request.getParameter("zhaiyao") != null && !"".equals(request.getParameter("zhaiyao"))){
			disjunctionOr.add(Restrictions.like("zhaiyou", "%"+request.getParameter("zhaiyao")+"%"));
		}
		criteria.add(disjunctionOr);
		
		String zhubuzhang = ToolKit.getProperties("com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource_zh_CN.properties", this)
		.getProperty("xingzhengshouwen.zhubuzhang");
		if (systemUserBo.isUserDuty(SessionUser.getUserId(request), zhubuzhang)) {
			Disjunction or = Restrictions.disjunction();
			if(SessionUser.getUser(request).getDepartment() != null){
				String department[] = SessionUser.getUser(request).getDepartment().split(",");
				for (String departmentid : department) {
					if(departmentid !=null &&!"".equals(departmentid)){
						or.add(Restrictions.like("jingshoubumen", "%,"+departmentid+",%"));
					}
				}
			}
			criteria.add(or.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%")));
		} 
		else{
			criteria.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%"));
		}
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		if (request.getParameter("start") == null) {
			list =   xingzhengshouwenNeirongBO.selectXingzhengshouwenPageCriteria(criteria, 0, 23);
		} else {
			Integer first = Integer.parseInt(request.getParameter("start"));
			list =   xingzhengshouwenNeirongBO.selectXingzhengshouwenPageCriteria(criteria, first, 23);
		}
		criteria = DetachedCriteria.forClass(TXingzhengshouwenNeirong.class);
		 zhubuzhang = ToolKit.getProperties("com/tjsinfo/tjsoa/documentationflow/xingzhengshouwen/properties/systemResource_zh_CN.properties", this)
		.getProperty("xingzhengshouwen.zhubuzhang");
		if (systemUserBo.isUserDuty(SessionUser.getUserId(request), zhubuzhang)) {
			Disjunction or = Restrictions.disjunction();
			if(SessionUser.getUser(request).getDepartment() != null){
				String department[] = SessionUser.getUser(request).getDepartment().split(",");
				for (String departmentid : department) {
					if(departmentid !=null &&!"".equals(departmentid)){
						or.add(Restrictions.like("jingshoubumen", "%,"+departmentid+",%"));
					}
				}
			}
			criteria.add(or.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%")));
		} 
		else{
			criteria.add(Restrictions.like("jingshouren", "%,"+SessionUser.getUserId(request)+",%"));
		}
		Integer totalProperty = xingzhengshouwenNeirongBO.selectXingzhengshouwenTotalLineCriteria(criteria);
		object.put("totalProperty", totalProperty);
		for (TXingzhengshouwenNeirong xingzhengshouwenNeirong : list) {
			JSONObject shouwenJSON = new JSONObject();
			if (workFlowTemplate.isEnd(Long.parseLong(xingzhengshouwenNeirong.getFlowid().toString()) )) {
				shouwenJSON.put("state", "是");
			} 
			else if (workFlowTemplate.isSuspend(Long.parseLong(xingzhengshouwenNeirong.getFlowid().toString()) )) {
				shouwenJSON.put("state", "中止");
			}
			else {
				shouwenJSON.put("state", "否");
			}
			shouwenJSON.put("laiwenzihao", xingzhengshouwenNeirong.getLaiwenzihao());
			shouwenJSON.put("laiwenjihuan", xingzhengshouwenNeirong.getLaiwenjiguan());
			shouwenJSON.put("zongshouwenhao", xingzhengshouwenNeirong.getShouwenbianhao());
			shouwenJSON.put("zhaiyao", xingzhengshouwenNeirong.getZhaiyou());
			shouwenJSON.put("shouwenriqi", ToolKit.FormatDate(xingzhengshouwenNeirong.getLaiwenriqi()));
			shouwenJSON.put("operate", "<a href='"+basePath+"documentationflow/xingzhengshouwen/genzong.jsp?flowid="+xingzhengshouwenNeirong.getFlowid()+
					"'>跟踪</a>&nbsp;&nbsp;<a href='"+basePath+"tjsinfo/tjsoa/xingzhengshouwen/shenpi.do?method=selectonedoc&id="+xingzhengshouwenNeirong.getId()+"'>查看</a>");
			array.put(shouwenJSON);
		}
		request.getSession().setAttribute("xingzhengshoujingshourenliststart", Integer.parseInt(request.getParameter("start")));
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
	
}
